package edu.gmu.swe632.fruit.activity;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import edu.gmu.swe632.fruit.R;
import edu.gmu.swe632.fruit.service.GestureRecognizerService;
import edu.gmu.swe632.fruit.util.CustomAdapter;
import edu.gmu.swe632.fruit.util.RowData;
import event.GestureEvent;

public class FireSizeActivity extends GestureListActivity
{
	private static final String TAG = "firesize";
	private static final String[] title = new String[] { "Small Fire", "Medium Fire", "Large Fire" };
	private static final String[] detail = new String[] { "", "", "" };
	private static final Integer[] imgid = { R.drawable.triangle, R.drawable.square,
			R.drawable.circle };
	private static final Class<?> nextIntentClasses[] = { FireDistanceActivity.class,
			FireDistanceActivity.class, FireDistanceActivity.class };

	private LayoutInflater mInflater;
	private Vector<RowData> data;
	private RowData rd;
	private Bundle bundle;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.list);
		mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		data = new Vector<RowData>();
		for (int i = 0; i < title.length; i++)
		{
			try
			{
				rd = new RowData(i, title[i], detail[i]);
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
			data.add(rd);
		}

		CustomAdapter adapter = new CustomAdapter(this, mInflater, R.layout.list_item, R.id.title,
				data, imgid);
		setListAdapter(adapter);
		getListView().setTextFilterEnabled(true);
		bundle = new Bundle();
	}

	@Override
	public void onListItemClick(ListView parent, View v, int position, long id)
	{
		startNextActivity(position);
	}

	@Override
	public void gestureReceived(GestureEvent event)
	{
		if (event.getProbability() > 0.9)
		{
			String gesture = GestureRecognizerService.GestureIdMapping.get(event.getId());

			Log.d(TAG, "Gesture received " + gesture);

			if (gesture.compareTo(GestureRecognizerService.TRIANGLE_GESTURE) == 0)
				startNextActivity(0);
			else if (gesture.compareTo(GestureRecognizerService.SQUARE_GESTURE) == 0)
				startNextActivity(1);
			else if (gesture.compareTo(GestureRecognizerService.CIRCLE_GESTURE) == 0)
				startNextActivity(2);
			else if (gesture.compareTo(GestureRecognizerService.SOS_GESTURE) == 0)
				startActivity(new Intent(this, SosActivity.class));
		}
	}

	private void startNextActivity(int index)
	{
		if (0 <= index && index < nextIntentClasses.length)
		{
			bundle.putString(FruitMainActivity.FIRE_SIZE, title[index]);

			Intent intent = new Intent(this, nextIntentClasses[index]).putExtras(bundle);
			startActivity(intent);
		}
	}
}
